<?php

namespace App\Http\Controllers\Personal;

use App\Model\Group_member;
use App\Model\Leave;
use App\Model\Leave_type;
use App\Model\Role;
use App\Model\Users;
use App\Model\Workflow;
use App\Model\Workflow_desc;
use App\Model\Workflow_group;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;

//中止申请
class LeaveController extends CommonController
{
	//
	protected $leave;
	protected $Users;
	protected $Leave_type;
	protected $Role;
	protected $Workflow;
	protected $Workflow_desc;
	protected static $company_id;
	protected static $uid;
	protected static $userInfo;
	protected $Workflow_group;
	protected $Group_member;

	public function __construct()
	{
		parent::__construct();
		$this->Users = new Users();
		$this->leave = new Leave();
		$this->Role = new Role();
		$this->Workflow = new Workflow();
		$this->Workflow_desc = new Workflow_desc();
		$this->Leave_type = new Leave_type();
		self::$company_id = Auth::user()->company_id;
		self::$uid = Auth::user()->id;
		self::$userInfo = Auth::user();
		$this->Workflow_group = new Workflow_group();
		$this->Group_member = new Group_member();
	}


//	中止申请列表
	public function index()
	{
		$uid = Auth::user()->id;
		$username=self::$userInfo->name;
		$data = $this->Users->user_staff_leave($uid);
		$newArr = [];
		$status = ['申请中', '已同意', '不通过'];
		foreach ($data['staff'] as $kk => $vv) {
			if ($vv['staff_leave']) {
				$newArr[$kk]['uid']=$uid;
				$newArr[$kk]['username']=$username;
				$newArr[$kk]['created_at'] = date('Y-m-d', strtotime($vv['staff_leave']['created_at']));
				$newArr[$kk]['task_name'] = $vv['store_station']['name'];
				$newArr[$kk]['time'] = $vv['staff_leave']['time'];
				$newArr[$kk]['start_time'] = $vv['staff_leave']['start_time'];
				$newArr[$kk]['end_time'] = $vv['staff_leave']['end_time'];
				$newArr[$kk]['status'] = $status[$vv['staff_leave']['status'] - 1];
			}
		}

		return $this->success($newArr);
	}


//	添加中止申请
	public function add_leave(Request $request)
	{
		DB::beginTransaction();
		$data = $request->only($this->leave->getFillable());
		$this->validate($request,[
			'staff_id' => 'required|integer',
			'name'=>'required|String',
			'staff_task_id'=>'required|integer',
			'type' => 'required|integer',
			'start_time'=>'required|date',
			'end_time'=>'required|date',
			'comment' =>'required|String',
			'store_station_id'=>'required',
		]);
		try {
			$data['time'] = (strtotime($request->end_time) - strtotime($request->start_time)) / 3600;
			$data['status'] = 1;
			$data['store_id'] = $this->store_id;
			$data['uid']=self::$uid;
			$result = $this->leave->create($data);
			$id = $result->id;
			$this->binding($id);
			DB::commit();
		} catch (\Exception $exception) {
			$message = $exception->getMessage();
			DB::rollBack();
		}
		if (!isset($message)) {
			return $this->stored($result);
		} else {
			return $this->failed($message);
		}
	}

//	获取用户所有的类型列表
	public function type_list()
	{
		return $this->Leave_type->get();
	}

//	获取该用户所有任务
	public function leave_task()
	{
		$where['uid'] = Auth::user()->id;
		$data = $this->Users->user_all_task($where);
		$newArr = [];
		foreach ($data['staffs'] as $kk => $vv) {
			if ($vv['staff_task'] != null) {
				$newArr[$kk]['staff_name'] = $vv['name'];
				$newArr[$kk]['staff_id'] = $vv['staff_task']['staff_id'];
				$newArr[$kk]['staff_task_id'] = $vv['staff_task']['id'];
				$newArr[$kk]['name'] = $vv['staff_task']['store_station']['name'];
				$newArr[$kk]['store_station_id'] = $vv['staff_task']['store_station']['id'];
			}
		}
		if (is_array($newArr) && count($newArr) > 0) {
			$newArr = array_values($newArr);
		}
		return $this->success($newArr);
	}


	


//	绑定审核流程
	public function binding($id)
	{
		$workflowData = $this->Workflow->where(['company_id' => self::$company_id, 'style' => 2, 'status' => 1])
			->first();
		$group_id = $this->belongsToGroup(self::$userInfo->department);
//		查询该用户的审批流
		$workflow_id = $this->Workflow_group->getworkflow_id($group_id, 2);
		$workflow_desc['lovel_uid'] = $this->Role->Role_user($workflowData->one);
		$workflow_desc['code'] = Uuid::uuid4();
		foreach ($workflow_desc['lovel_uid'] as $kk => $vv) {
			$data[$kk]['workflow_id'] = $workflowData->id;
			$data[$kk]['from_uid'] = self::$uid;
			$data[$kk]['lovel_role'] = $workflowData->one;
			$data[$kk]['lovel_uid'] = $vv;
			$data[$kk]['leave_id'] = $id;
			$data[$kk]['code'] = $workflow_desc['code'];
			$data[$kk]['type'] = 2;
			$data[$kk]['created_at'] = date("Y-m-d H:i:s", time());
		}
		DB::table('workflow_desc')->insert($data);
	}


}
